const Router = require('koa-router')
const { sequelize } = require('../database/db')
const { QueryTypes } = require('sequelize')
const check = require('../pem/check')

let router = new Router()

router.get('/list', async(ctx) => {
    const data = await sequelize.query(
        `SELECT * FROM Links`,
        { type: QueryTypes.SELECT }
    );
    ctx.body = {
        code: 101,
        data,
        msg: ''
    }
})

router.post('/add', async(ctx) => {
    const { link, title } = ctx.request.body
    if (title && link) {
        const verify = await check(ctx.request.headers.authorization)
        if (verify) {
            const data = await sequelize.query(
                `INSERT INTO Links (link, title) VALUES ('${link}', '${title}')`
            );
            if (data.length && data[1] == 1) {
                ctx.body = {
                    code: 101,
                    data: {
                        id: data[0],
                        link,
                        title
                    },
                    msg: '成功'
                }
            } else {
                ctx.body = {
                    code: 102,
                    data: null,
                    msg: ''
                }
            }
        } else {
            ctx.body = {
                code: 201,
                data: null,
                msg: 'token过期'
            }
        }
    } else {
        ctx.body = {
            code: 102,
            data: null,
            msg: ''
        }
    }
})

router.post('/update', async(ctx) => {
    const { link, title, id } = ctx.request.body
    if (title && id && link) {
        const verify = await check(ctx.request.headers.authorization)
        if (verify) {
            const data = await sequelize.query(
                `UPDATE Links SET title = '${title}', link = '${link}' WHERE id = ${id}`
            );
            if (data) {
                ctx.body = {
                    code: 101,
                    data: {
                        id,
                        link,
                        title
                    },
                    msg: '成功'
                }
            } else {
                ctx.body = {
                    code: 102,
                    data: null,
                    msg: ''
                }
            }
        } else {
            ctx.body = {
                code: 201,
                data: null,
                msg: 'token过期'
            }
        }
    } else {
        ctx.body = {
            code: 102,
            data: null,
            msg: ''
        }
    }
})

router.post('/batchDel', async(ctx) => {
    const { id } = ctx.request.body
    if (id.length) {
        const verify = await check(ctx.request.headers.authorization)
        if (verify) {
            const data = await sequelize.query(
                `DELETE FROM Links WHERE id IN (${id.join(',')})`
            );
            if (data) {
                ctx.body = {
                    code: 101,
                    data: id,
                    msg: '成功'
                }
            } else {
                ctx.body = {
                    code: 102,
                    data: null,
                    msg: ''
                }
            }
        } else {
            ctx.body = {
                code: 201,
                data: null,
                msg: 'token过期'
            }
        }
    } else {
        ctx.body = {
            code: 102,
            data: null,
            msg: ''
        }
    }
})

module.exports = router
